package com.hiultra.archive.controller;

import com.hiultra.archive.domain.ArcArchive;
import com.hiultra.archive.domain.ArcBox;
import com.hiultra.archive.service.IArcArchiveService;
import com.hiultra.archive.service.IArcBoxService;
import com.hiultra.archive.service.IArcDossierService;
import com.hiultra.archive.service.IArcFamilyService;
import com.hiultra.common.annotation.Log;
import com.hiultra.common.constant.UserConstants;
import com.hiultra.common.core.controller.BaseController;
import com.hiultra.common.core.domain.AjaxResult;
import com.hiultra.common.core.page.TableDataInfo;
import com.hiultra.common.enums.BusinessType;
import com.hiultra.common.utils.StringUtils;
import com.hiultra.common.utils.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 档案Controller
 * 
 * @author ls
 * @date 2022-07-30
 */
@Api(tags="档案管理Controller")
@RestController
@RequestMapping("/archive/archive")
public class ArcArchiveController extends BaseController
{
    @Autowired
    private IArcArchiveService arcArchiveService;
    @Autowired
    private IArcFamilyService arcFamilyService;
    @Autowired
    private IArcDossierService arcDossierService;
    @Autowired
    private IArcBoxService arcBoxService;
    /**
     * 查询档案列表
     */
    @ApiOperation(value = "查询档案列表", notes = "查询档案列表")
    @PreAuthorize("@ss.hasPermi('archive:archive:list')")
    @GetMapping("/list")
    public TableDataInfo list(ArcArchive arcArchive)
    {
        startPage();
        List<ArcArchive> list = arcArchiveService.selectArcArchiveList(arcArchive);
        return getDataTable(list);
    }

    /**
     * 导出档案列表
     */
    @ApiOperation(value = "导出档案列表", notes = "导出档案列表")
    @PreAuthorize("@ss.hasPermi('archive:archive:export')")
    @Log(title = "档案", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ArcArchive arcArchive)
    {
        List<ArcArchive> list = arcArchiveService.selectArcArchiveList(arcArchive);
        ExcelUtil<ArcArchive> util = new ExcelUtil<ArcArchive>(ArcArchive.class);
        util.exportExcel(response, list, "档案数据");
    }

    /**
     * 获取档案详细信息
     */
    @ApiOperation(value = "获取档案详细信息", notes = "获取档案详细信息")
    @PreAuthorize("@ss.hasPermi('archive:archive:query')")
    @GetMapping(value = { "/", "/{archiveId}" })
    public AjaxResult getInfo(@PathVariable(value = "archiveId", required = false) Long archiveId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("familys", arcFamilyService.selectArcFamilyList(null));
        ajax.put("dossiers", arcDossierService.selectArcDossierList(null));
        ajax.put("boxs", arcBoxService.selectArcBoxList(null));
        if (StringUtils.isNotNull(archiveId)) {
            ArcArchive arcArchive = arcArchiveService.selectArcArchiveByArchiveId(archiveId);
            ajax.put(AjaxResult.DATA_TAG, arcArchive);
        }
        return ajax;
    }

    /**
     * 新增档案
     */
    @ApiOperation(value = "新增档案", notes = "新增档案")
    @PreAuthorize("@ss.hasPermi('archive:archive:add')")
    @Log(title = "档案", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ArcArchive arcArchive)
    {
        if(UserConstants.NOT_UNIQUE.equals(arcArchiveService.checkArchiveCodeUnique(arcArchive))){
            return AjaxResult.error("新增档案'"+arcArchive.getArchiveCode()+"'失败，档案档号已存在！");
        }
        return toAjax(arcArchiveService.insertArcArchive(arcArchive));
    }

    /**
     * 修改档案
     */
    @ApiOperation(value = "修改档案", notes = "修改档案")
    @PreAuthorize("@ss.hasPermi('archive:archive:edit')")
    @Log(title = "档案", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ArcArchive arcArchive)
    {
        if(UserConstants.NOT_UNIQUE.equals(arcArchiveService.checkArchiveCodeUnique(arcArchive))){
            return AjaxResult.error("修改档案'"+arcArchive.getArchiveCode()+"'失败，档案档号已存在！");
        }
        return toAjax(arcArchiveService.updateArcArchive(arcArchive));
    }

    /**
     * 删除档案
     */
    @ApiOperation(value = "删除档案", notes = "删除档案")
    @PreAuthorize("@ss.hasPermi('archive:archive:remove')")
    @Log(title = "档案", businessType = BusinessType.DELETE)
	@DeleteMapping("/{archiveIds}")
    public AjaxResult remove(@PathVariable Long[] archiveIds)
    {
        return toAjax(arcArchiveService.deleteArcArchiveByArchiveIds(archiveIds));
    }

    /**
     * 下载模板
     */
    @ApiOperation(value = "下载模板", notes = "下载模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<ArcArchive> util = new ExcelUtil<ArcArchive>(ArcArchive.class);
        util.importTemplateExcel(response, "档案数据");
    }

    /**
     * 导入档案数据
     */
    @ApiOperation(value = "导入档案数据", notes = "导入档案数据")
    @Log(title = "档案管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('archive:archive:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<ArcArchive> util = new ExcelUtil<ArcArchive>(ArcArchive.class);
        List<ArcArchive> archiveList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = arcArchiveService.importArchive(archiveList, updateSupport, operName);
        return AjaxResult.success(message);
    }
}
